package com.share.finance.risk.core.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.share.common.common.Page;
import com.share.finance.risk.common.model.sys.PreRequest;
import com.share.finance.risk.common.model.sys.RuleNode;
import com.share.finance.risk.common.model.vo.BaseCustomerInfo;
import com.share.finance.risk.core.mapper.ResultInfoMapper;
import com.share.finance.risk.core.model.db.ResultInfo;
import com.share.finance.risk.core.model.vo.ResultPageReq;
import com.share.finance.risk.core.service.IResultInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 风控结果记录  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-12-26
 */
@Service
public class ResultInfoServiceImpl extends ServiceImpl<ResultInfoMapper, ResultInfo> implements IResultInfoService {
    @Resource
    private ResultInfoMapper resultInfoMapper;

    @Override
    public Page pageList(ResultPageReq req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        LambdaQueryWrapper<ResultInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(req.getName())) {
            queryWrapper.like(ResultInfo::getName, req.getName());
        }
        if (StringUtils.isNotBlank(req.getResultType())) {
            queryWrapper.like(ResultInfo::getResultType, req.getResultType());
        }
        if (StringUtils.isNotBlank(req.getIdCard())) {
            queryWrapper.like(ResultInfo::getIdCard, req.getIdCard());
        }
        if (StringUtils.isNotBlank(req.getPhoneNo())) {
            queryWrapper.like(ResultInfo::getPhoneNo, req.getPhoneNo());
        }
        if (StringUtils.isNotBlank(req.getEventType())) {
            queryWrapper.like(ResultInfo::getEventType, req.getEventType());
        }
        if (StringUtils.isNotBlank(req.getCreditLimit())) {
            queryWrapper.like(ResultInfo::getCreditLimit, req.getCreditLimit());
        }
        queryWrapper.orderByDesc(ResultInfo::getId);
        List<ResultInfo> list = resultInfoMapper.selectList(queryWrapper);
        return new Page<>(list, new PageInfo<>(list).getTotal(), req);
    }

    @Override
    public int saveReultInfo(PreRequest req, RuleNode ruleJson, long proceTime) {
        ResultInfo po = new ResultInfo();
        po.setProcessTime(proceTime);
        po.setCreateTime(new Date());
        po.setProductCode(req.getProductCode());
        po.setEventType(req.getEventType());
        po.setSessionId(req.getSessionId());
        po.setStatus(1);

        Object data = req.getData();
        BaseCustomerInfo base = JSON.parseObject(JSON.toJSONString(data), BaseCustomerInfo.class);
        po.setName(base.getName());
        po.setPhoneNo(base.getPhoneNo());
        po.setIdCard(base.getIdCard());

        if (ruleJson != null) {
            po.setResultType(ruleJson.getResultType());
            po.setCreditLimit(ruleJson.getCreditLimit());
        }
        return resultInfoMapper.insert(po);
    }
}
